package group.igfd.manager.biz.controller;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.ArrayStack;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import group.igfd.manager.biz.entity.BrandsEntity;
import group.igfd.manager.biz.service.BrandsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 品牌表
 *
 * @author knight
 * @email sunlightcs@gmail.com
 * @date 2018-10-17 13:49:59
 */
@RestController
@RequestMapping("biz/brands")
public class BrandsController {
    @Autowired
    private BrandsService brandsService;
    //这是测试冲突
    private Logger logger = LoggerFactory.getLogger(BrandsController.class);

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("biz:brands:list")
    public R list(@RequestParam Map<String, Object> params){
        logger.info(params.toString());

        PageUtils page = brandsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("biz:brands:info")
    public R info(@PathVariable("id") Integer id){
			BrandsEntity brands = brandsService.selectById(id);
        //adbc
        //ddd
        return R.ok().put("brands", brands);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("biz:brands:save")
    public R save(@RequestBody BrandsEntity brands){
        try {

            brands.setName(brands.getName().toUpperCase());
            brands.setFix(brands.getName().substring(0,1));

            Date now = new Date();
            brands.setAuthFlag(0);
            brands.setCreatedAt(now);
            brands.setUpdatedAt(now);
            brandsService.insert(brands);

            return R.ok();
        }catch (Exception e){
            return R.error(400, "请求失败");
        }

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("biz:brands:update")
    public R update(@RequestBody BrandsEntity brands){
            //修改
            brands.setName(brands.getName().toUpperCase());
            brands.setFix(brands.getName().substring(0,1));

            Date now = new Date();
            brands.setCreatedAt(now);
            brands.setUpdatedAt(now);

			brandsService.updateById(brands);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("biz:brands:delete")
    public R delete(@RequestBody Integer[] ids){
        List<BrandsEntity> list = brandsService.selectBatchIds(Arrays.asList(ids));
        logger.info(list.toString());
        for (BrandsEntity brand: list ) {
            logger.info(brand.getName());
            brand.setDelFlag(1);
        }
        brandsService.updateBatchById(list);
//			brandsService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 审核
     * */
    @RequestMapping("/auth/{id}")
    @RequiresPermissions("biz:brands:auth")
    public R auth(@PathVariable("id") Integer id){

        BrandsEntity brands = brandsService.selectById(id);
        brands.setAuthFlag(1);
        brandsService.updateById(brands);
        return R.ok();
    }

}
